package com.alog.yzd.jdbc.hm;

import com.alog.yzd.utils.Print;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class CRDU {
    public static void main(String[] args) throws SQLException {
        //create();
        //read();
        //update();
        //delete();

        for(int i = 0 ; i < 10; i++){
            Connection conn = JdbcUtils2.getConnction();
            Print.print(conn.getClass().getName());
            JdbcUtils2.release(null, null, conn);
        }
    }

    static void create() throws SQLException {
        Connection conn = null;
        Statement st = null;
        try{
            conn = JdbcUtils2.getConnction();
            st = conn.createStatement();
            int i = st.executeUpdate("insert into user (id , name, pwd) values (6, 'Tony','xxxkkk');");
            Print.print("i = " + i);
        } finally {
            JdbcUtils2.release(null, st, conn);
        }
    }

    static void read()throws SQLException{
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try{
            conn = JdbcUtils2.getConnction();
            st = conn.createStatement();
            rs = st.executeQuery("select id, name, pwd from user;");
            while(rs.next()){
                Print.print("id = " + rs.getObject("id") + "\tname = " + rs.getObject("name") + "\tpwd = " + rs.getObject("pwd"));
            }
        } finally {
            JdbcUtils2.release(null, st, conn);
        }
    }

    static void update() throws SQLException {
        Connection conn = null;
        Statement st = null;
        try{
            conn = JdbcUtils2.getConnction();
            st = conn.createStatement();
            int i = st.executeUpdate("update user set id = 5 where id = 6;");
            Print.print("i = " + i);
        } finally {
            JdbcUtils2.release(null, st, conn);
        }
    }

    static void delete() throws SQLException {
        Connection conn = null;
        Statement st = null;
        try{
            conn = JdbcUtils2.getConnction();
            st = conn.createStatement();
            int i = st.executeUpdate("delete from user where id = 5;");
            Print.print("i = " + i);
        } finally {
            JdbcUtils2.release(null, st, conn);
        }
    }
}
